package com.hy.controller;

import com.hy.admin.pojo.RegistrationGrowth;
import com.hy.admin.pojo.RegistrationInfo;
import com.hy.admin.pojo.UserActivity;
import com.hy.service.UserActivityService;
import com.hy.util.common.Result;
import com.hy.util.common.ResultCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户活动统计接口")
@RestController
@RequestMapping("/admin/userActivity")
@Slf4j
public class UserActivityController {
    @Autowired
    private UserActivityService userActivityService;

    @ApiOperation("获取用户活动统计总数")
    @GetMapping("/calculateTotals")
    public Result<double[]> calculateTotals() {
        try {
            log.info("开始获取统计数据总和");
            double[] totals = userActivityService.calculateTotals();
            log.info("获取统计数据成功: [注册总数:{}, 活跃度总和:{}, 增长总数:{}]",
                    totals[0], totals[1], totals[2]);
            return Result.build(totals, ResultCodeEnum.SUCCESS);
        } catch (Exception e) {
            log.error("计算统计数据失败", e);
            return Result.build(401, "获取统计数据失败");
        }
    }

    @ApiOperation("获取最近6个月的注册信息")
    @GetMapping("/registrationInfos")
    public Result<List<RegistrationInfo>> getRegistrationInfos() {
        try {
            log.info("开始获取注册信息");
            List<RegistrationInfo> infos = userActivityService.getRegistrationInfos();
            log.info("获取注册信息成功，数据条数: {}", infos.size());
            return Result.build(infos, ResultCodeEnum.SUCCESS);
        } catch (Exception e) {
            log.error("获取注册信息失败", e);
            return Result.build(402, "获取注册信息失败");
        }
    }

    @ApiOperation("获取最近6个月的用户活跃度")
    @GetMapping("/userActivities")
    public Result<List<UserActivity>> getUserActivities() {
        try {
            log.info("开始获取用户活跃度数据");
            List<UserActivity> activities = userActivityService.getUserActivities();
            log.info("获取用户活跃度成功，数据条数: {}", activities.size());
            return Result.build(activities, ResultCodeEnum.SUCCESS);
        } catch (Exception e) {
            log.error("获取用户活跃度失败", e);
            return Result.build(403, "获取用户活跃度失败");
        }
    }

    @ApiOperation("获取最近6个月的注册增长数据")
    @GetMapping("/registrationGrowths")
    public Result<List<RegistrationGrowth>> getRegistrationGrowths() {
        try {
            log.info("开始获取注册增长数据");
            List<RegistrationGrowth> growths = userActivityService.getRegistrationGrowths();
            log.info("获取注册增长数据成功，数据条数: {}", growths.size());
            return Result.build(growths, ResultCodeEnum.SUCCESS);
        } catch (Exception e) {
            log.error("获取注册增长数据失败", e);
            return Result.build(404, "获取注册增长数据失败");
        }
    }
}
